package com.common.account.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.account.entity.AuUserLog;
import com.common.account.entity.vo.AuUserLogStatist;
import com.common.account.mapper.AuUserLogMapper;
import com.common.account.service.IAuUserLogService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户日志表 服务实现类
 * </p>
 *
 * @author mdc
 * @since 2021-11-12
 */
@Service
public class AuUserLogServiceImpl extends ServiceImpl<AuUserLogMapper, AuUserLog> implements IAuUserLogService {

    @Override
    public void saveUserOperationLog(String userId, String userName, String ipAddress) {
        this.save(new AuUserLog(userId, userName, ipAddress));
    }

    @Override
    public AuUserLogStatist statisticalAnalysis() {
        int count = count();
        AuUserLogStatist userLogStatist = new AuUserLogStatist();
        userLogStatist.setSum(count);
        LambdaQueryWrapper<AuUserLog> query = Wrappers.lambdaQuery();
        query.apply("CONCAT(YEAR(created),MONTH(created))=CONCAT(YEAR(now()),MONTH(now()))");
        int newMonth = this.count(query);
        userLogStatist.setNewMonth(newMonth);
        query.clear();
        query.apply("CONCAT(YEAR(created),MONTH(created))=CONCAT(YEAR(now()),MONTH(now())-1)");
        int addedLastMonth = this.count(query);
        userLogStatist.setAddedLastMonth(addedLastMonth);
        return userLogStatist;
    }
}
